<?php

/* ~ comment.class.php
  .---------------------------------------------------------------------------.
  |  Software: JCCP CMS                                                       |
  |   Version: 1.0                                                            |
  |      Site: https:www.jccpwebstudio.com                                    |
  | ------------------------------------------------------------------------- |
  |     Admin: Juan Carlos Contreras (project admininistrator)                |
  |   Authors: Juan C Contreras (jccp) jc@jccpwebstudio.com                   |
  |          : David Hartmann   (david) david@jccpwebstudio.com               |
  | ------------------------------------------------------------------------- |
  '---------------------------------------------------------------------------'
 */

/**
 * Description of comment
 *
 * @author Juan Carlos Contreras-Piana
 */
class comment {

    /**
     * ID of the comment.
     * This is the comment ID
     * @var int
     */
    public $id;

    /**
     * The user id that put the comment in.
     * @var int
     */
    public $user_id;

    /**
     * The id of the challenge
     * @var int
     */
    public $challenge_id;

    /**
     * The text of the comment.
     * @var text 
     */
    public $comment;

    /**
     * Date and time that the comment was posted
     * @var Mixed 
     */
    public $time_date;

    public function addComment() {

        $this->comment = mysql_real_escape_string($this->comment);
		date_default_timezone_set('America/New_York');
		
		
   $result = mysql_query("Insert into comments (comments_user_id, comments_challenge_id, comment, date_time) VALUES 
            (
            '" . $this->user_id . "',
                '" . $this->challenge_id . "',
                    '" . $this->comment . "',
                            '" . date("Y-m-d H:i:s") . "'
            )") or die(mysql_error());

        if ($result) {

            return TRUE;
        }
        return FALSE;
    }

    public function deleteComment($comment_id) {
        
         $result = mysql_query("Delete from comments where id='".$comment_id."'") or die(mysql_error());

        if ($result) {

            return TRUE;
        }
        return FALSE;
        
    }

    public function loadComments($column, $value) {
        
        $result=mysql_query("Select * from comments where ".$comment."='".$value."' ")or die(mysql_error());
        
        return mysql_fetch_object($result);
        
    }

    public function loadAllCommentsForUser($user_id) {
        
        $result=mysql_query("Select * from comments where comments_user_id='".$user_id."' ")or die(mysql_error());
        
        $rows=array();
        
        while($row=  mysql_fetch_object($result)){
            
            $rows[]=$row;
            
        }
        
        return $rows;
        
    }

    public function loadAllCommentForChallenge($challenge_id) {
        
        //$result=mysql_query("Select * from comments where comments_challenge_id='".$challenge_id."' ")or die(mysql_error());
	   $result=mysql_query("SELECT * FROM comments LEFT JOIN users ON comments.comments_user_id=users.id WHERE comments_challenge_id='".$challenge_id."' ")or die(mysql_error());
        
        $rows=array();
        
        while($row=mysql_fetch_object($result)){
            
            $rows[]=$row;
            
        }
		//print_r($rows);
        
        return $rows;
        
    }

}

?>
